package de.hdu.pms.ctrl;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import de.hdu.pms.dao.BasisDao;
import de.hdu.pms.model.Basis;

@Controller
public class BasisController {
	private static Logger Log = Logger.getLogger(BasisController.class);
	private BasisDao basisDao = null;

	public void setBasisDao(BasisDao basisDao) {
		this.basisDao = basisDao;
	}

	
	/**
	 * <p>
	 * Edits Basis.
	 * </p>
	 * <p>
	 * Expected HTTP GET and request '/EditBasis.html'.
	 * </p>
	 * <p>
	 * Linked Viewname to 'edit-basis.jsp'
	 * 
	 * @return mv
	 * @author BBV
	 **/
	@RequestMapping(value = "/EditBasis.html", method = RequestMethod.GET)
	public ModelAndView edit(@RequestParam(required = false) Integer id) {
		Log.debug("EditBasis.html");
		ModelAndView mv = new ModelAndView();
		if (id == null) {
			mv.addObject(new Basis());
		} else {
			mv.addObject(basisDao.findById(id));
		}
		mv.addObject("title", "Basis");
		mv.addObject("message", "Basis bearbeiten");
		mv.setViewName("edit-basis");
		return mv;
	}

	
	/**
	 * <p>
	 * Save Basis
	 * </p>
	 * <p>
	 * Expected HTTP Post and request '/SaveBasis.html'
	 * <p>
	 * redirects to '/Basis.html'
	 * 
	 * @param basis
	 * @param model
	 * @return
	 * @author BBV
	 */
	@RequestMapping(value = "/SaveBasis.html", method = RequestMethod.POST)
	public String save(Basis basis, Model model) {
		basisDao.save(basis);
		model.addAttribute("statusMessageKey", "projekte.form.msg.success");
		return "redirect:Basis.html";
	}

	
	/**
	 * <p>
	 * Delets Basis
	 * </p>
	 * <p>
	 * Expected HTTP GET and request for '/DeleteBasis.html'
	 * <p>
	 * redirects to '/Basis.html'
	 * 
	 * @param basis
	 * @return
	 * @author BBV
	 */
	@RequestMapping(value = "/DeleteBasis.html", method = RequestMethod.GET)
	public String delete(Basis basis) {
		basisDao.delete(basis);
		return "redirect:Basis.html";
	}

	
	/**
	 * <p>
	 * Lists Basis
	 * </p>
	 * <p>
	 * Expected HTTP GET and request for '/Basis.html'
	 * <p>
	 * Linked Viewname to 'list-basis.jsp'
	 * 
	 * @return mv
	 * @author BBV
	 */
	@RequestMapping(value = "/Basis.html", method = RequestMethod.GET)
	public ModelAndView findAll() {
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Basis");
		mv.addObject("message", "Verwaltung von Basiszeiten");
		mv.addObject("basis", basisDao.findAll());
		mv.setViewName("list-basis");
		return mv;
	}

}
	 


